package com.wewoo.log4e.starter.action;

import com.wewoo.log4e.core.Register.ReadRegister;
import com.wewoo.log4e.core.entity.OperationLog;
import com.wewoo.log4e.core.exception.Log4eException;
import com.wewoo.log4e.core.util.ReadLogAbstractUtil;
import lombok.Builder;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

@Builder
public class ReadLogDataBaseUtil extends ReadLogAbstractUtil<OperationLog> {
    public static final Logger LOGGER = LoggerFactory.getLogger(ReadLogDataBaseUtil.class);
    @Setter
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<String> readLogList() {
        LOGGER.info("==============================database log util start read log data==============================");
        List<String> stringList = jdbcTemplate.queryForList("select descripition from operation_log ",java.lang.String.class);
        LOGGER.info("show sql : {}","select descripition from operation_log ");
        return stringList;
    }

    @Override
    public List<OperationLog> readLogList(OperationLog operationLog) {
        LOGGER.info("==============================database log util start read log data==============================");
        if(null==operationLog){
            throw new Log4eException("param operationLog is null ");
        }
        List<OperationLog> queryForObject = jdbcTemplate.query("select * from operation_log where relate_unique=? ",new BeanPropertyRowMapper<>(OperationLog.class),operationLog.getRelateUnique());
        LOGGER.info("show sql : {}","select * from operation_log where relate_unique=? ");
        LOGGER.info("param : {}",operationLog.getRelateUnique());
        return queryForObject;
    }
}
